﻿#region File Description
//-----------------------------------------------------------------------------
// SingleAnimationTask
//
// Copyright © 2013 Wave Coorporation. All rights reserved.
// Use is subject to license terms.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using WaveEngine.Common.Media;
using WaveEngine.Framework;
using WaveEngine.Framework.Animation;
using WaveEngine.Framework.Services;
#endregion

namespace WaveEngine.Extensions.ITasks
{
    public class VideoTask : TaskBase
    {
        /// <summary>
        /// Gets or sets the video info.
        /// </summary>
        /// <value>
        /// The video info.
        /// </value>
        public VideoInfo VideoInfo { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="VideoTask" /> class.
        /// </summary>
        /// <param name="scene">The scene.</param>
        /// <param name="taskName">Name of the task.</param>
        /// <param name="videoInfo">The video info.</param>
        public VideoTask(Scene scene, string taskName, VideoInfo videoInfo)
            : base(scene, taskName)
        {
            this.VideoInfo = videoInfo;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="VideoTask" /> class.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="taskName">Name of the task.</param>
        /// <param name="videoInfo">The video info.</param>
        public VideoTask(ITask parent, string taskName, VideoInfo videoInfo)
            : base(parent, taskName)
        {
            this.VideoInfo = videoInfo;
        }

        /// <summary>
        /// Ons the run.
        /// </summary>
        protected override void performRun()
        {
            WaveServices.VideoPlayer.OnComplete += video_Completed;
            WaveServices.VideoPlayer.Play(this.VideoInfo);
        }

        /// <summary>
        /// Handles the Completed event of the singleAnimation control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void video_Completed(object sender, EventArgs e)
        {
            //WaveServices.VideoPlayer.Stop();
            WaveServices.VideoPlayer.OnComplete -= video_Completed;
            this.performCompleted();
        }

        /// <summary>
        /// Notifies the cancelled.
        /// </summary>
        protected override void performCancel()
        {
            WaveServices.VideoPlayer.OnComplete -= video_Completed;
            WaveServices.VideoPlayer.Stop();
            base.performCancel();
        }

        /// <summary>
        /// Notifies the skip.
        /// </summary>
        /// <returns></returns>
        protected override bool performSkip()
        {
            if (this.IsSkippable)
            {
                this.Cancel();
                return base.performSkip();
            }
            else
                return false;
        }

    }
}
